package com.ghc.whiteboxtest;

import android.app.Instrumentation;
import android.app.UiAutomation;
import android.graphics.Rect;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiCollection;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.util.Log;
import android.view.InputDevice;
import android.view.InputEvent;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;

import junit.framework.TestCase;

import java.io.IOException;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static java.lang.Thread.sleep;


public class ExampleInstrumentedTest extends TestCase {
    private final static String TAG = "ExampleInstrumentedTest";
    Instrumentation instrumentation = getInstrumentation();
    UiDevice uiDevice = UiDevice.getInstance(instrumentation);

    public void testCameThumb100() throws UiObjectNotFoundException {
        for (int i = 0; i < 1000; i++) {
            Log(TAG, "ghc testCameThumb100:" + i);
            testCamThumb();
        }
    }

    // 长按2S录像，然后保存，然后点击进入相册，然后返回到相机。
    public void testCamThumb() throws UiObjectNotFoundException {
        UiCollection allViews = new UiCollection(new UiSelector()
                .className("android.widget.FrameLayout"));

        // 拍照按键
        UiObject ui_shutter = allViews.getChild(
                new UiSelector().className("android.widget.ImageView")
                        .resourceId("com.transsion.camera:id/shutter_button"));

        // 缩略图按钮
        UiObject btn_thumbil = allViews.getChild(
                new UiSelector().className("android.view.View")
                        .resourceId("com.transsion.camera:id/view_thumbnail_bg"));
        Log(TAG, "ghc testCamThumb:");

        try {
            // 长按录像
            Rect stRect = ui_shutter.getVisibleBounds();
            int nPressX = stRect.centerX();
            int nPressY = stRect.centerY();
            //100对应着0.5秒
            int nSecondsSteps = 200;
            uiDevice.swipe(nPressX, nPressY, nPressX, nPressY, nSecondsSteps);
            Log(TAG, "ghc testCamThumb:" + nPressX + ";" + nPressY);

            // 进入图库
//            uiDevice.waitForIdle(2000);
//            btn_thumbil.clickAndWaitForNewWindow();

            // 返回相机
//            uiDevice.pressBack();

        } catch (UiObjectNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    // AR模式快速切换贴纸效果后，拍照后home键退出，再次进入，缩略图效果与图片效果不一致
    // http://10.150.1.85:8080/browse/CCJHLEL-526
    public void testAR() throws UiObjectNotFoundException {
        UiCollection allViews = new UiCollection(new UiSelector()
                .className("android.widget.FrameLayout"));

        // 拍照按键
        UiObject ui_shutter = allViews.getChild(
                new UiSelector().className("android.widget.ImageView")
                        .resourceId("com.transsion.camera.debug:id/shutter_button"));

        try {
            // 长按录像
            Rect stRect = ui_shutter.getVisibleBounds();
            int nPressX = stRect.centerX();
            int nPressY = stRect.centerY();
            //100对应着0.5秒
            int nSecondsSteps = 5;
            uiDevice.swipe(nPressX, nPressY, nPressX, nPressY, nSecondsSteps);
            Log(TAG, "ghc testAR:" + nPressX + ";" + nPressY);
            SystemClock.sleep(3);
            Log(TAG, "ghc testAR presssHome");
            uiDevice.pressHome();

        } catch (UiObjectNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    private void Log(String TAG, String content) {
        System.out.println(TAG + content);
    }
}
